<template>
  <div>
    <!-- 表格 -->
    <el-table style="width: 100%" border :data="skuList">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <el-table-column prop="prop" label="默认图片" width="110">
        <template slot-scope="{row,$index}">
          <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="80">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="80">
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" icon="el-icon-top" size="mini" v-if="row.isSale == 1"
            @click="Sale(row)"></el-button>
          <el-button type="success" icon="el-icon-bottom" size="mini" v-else @click="CancelSale(row)"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="$message('正在开发中')"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
      :page-sizes="[3, 5, 10]" :page-size="limit" layout="prev, pager, next, jumper,->,total, sizes" :total="total"
      style="text-align:center">
    </el-pagination>
    <!-- 抽屉 -->
    <!-- show-close是否显示关闭,direction方向从右往左 -->
    <el-drawer :visible.sync="drawer" :show-close="false" size="50%">
      <el-form label-width="80px">
        <el-row>
          <el-col :span="5">名称</el-col>
          <el-col :span="16">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">描述</el-col>
          <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">价格</el-col>
          <el-col :span="16">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">平台属性</el-col>
          <el-col :span="16">
            <el-tag type="success" v-for="(attr, index) in skuInfo.skuAttrValueList" :key="attr.attrId"
              style="margin-right:10px">{{ attr.attrId }}-{{ attr.valueId }}</el-tag>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">商品图片</el-col>
          <el-col :span="16">
            <!-- carousel走马灯 -->
            <el-carousel trigger="click" height="500px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="" style="width:500px;height:500px">
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'sku',
  data() {
    return {
      // 分页器相关
      page: 1,
      total: 0,
      limit: 3,

      // 获取sku列表
      skuList: [],

      // 保存sku信息
      skuInfo: {},

      // 控制sku详情界面的抽屉打开
      drawer: false
    }
  },
  methods: {
    // 分页器相关
    handleCurrentChange(page) {
      this.page = page
      this.getSkuList()
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },

    // 获取sku列表
    async getSkuList() {
      let { page, limit } = this
      let result = await this.$API.sku.reqGetSkuList(page, limit)
      if (result.code == 200) {
        this.skuList = result.data.records
        this.total = result.data.total
      }
    },

    // 上架和下架相关方法
    async Sale(sku) {
      let result = await this.$API.sku.reqSale(sku.id)
      if (result.code == 200) {
        this.$message({ type: "success", message: "上架成功" })
        sku.isSale = 0
      }
    },
    async CancelSale(sku) {
      let result = await this.$API.sku.reqCancelSale(sku.id)
      if (result.code == 200) {
        this.$message({ type: "success", message: "下架成功" })
        sku.isSale = 1
      }
    },

    // 获取sku信息
    async getSkuInfo(sku) {
      let result = await this.$API.sku.reqSkuById(sku.id)
      if (result.code == 200) {
        this.skuInfo = result.data
      }
      // 显示抽屉
      this.drawer = true

    }
  },
  mounted() {
    // 挂载时获取一次sku列表
    this.getSkuList()
  }
}
</script>

<style scoped>
/* 栅格系统相关样式 */
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}

.el-col {
  margin: 10px 10px;
}
</style>